new Vue({
    el: "#app",
    data() {
        return {
            loginForm: {
                couName: "",
                couPassword: ""
            },
            loginRules: {
                couName: [
                    {required: true, trigger: "blur", message: "用户名不能为空"}
                ],
                couPassword: [
                    {required: true, trigger: "blur", message: "密码不能为空"}
                ]
            },
            loading: false
        };
    },
    methods: {
        handleLogin() {
            if(!this.loading){
                this.$refs.loginForm.validate(valid => {
                    if (valid) {
                        this.loading = true;
                        this.login(this.loginForm);
                    }
                });
            }
        },
        login(user) {
            this.loading = true
            userLogin(user).then(res => {
                if(res.code === 200) {
                    localStorage.setItem('userInfo',JSON.stringify({...this.loginForm,couNickname: res.data.couNickname,couPoliceNo: res.data.couPoliceNo}))
                    location.href = ('../index.html')
                }
            }).catch(() => {
                this.loading = false
            })
        }
    }
})